Skip to content

Conversation

@mhucka
Copy link
Collaborator

@mhucka mhucka commented Jan 6, 2026

It looks like it's possible to use the -mbmi2 flag when -mavx2 is used, based on documentation and on testing locally. This was done in some Makefiles but not consistently. This PR adds the flag to Makefiles where it was missing, plus also in one BUILD file.

The `-mbmi2` flag can be used in conjunction with `-mavx2`. This was
done in some places but not consistently.
@github-actions github-actions bot added the Size: XS <10 lines changed label Jan 6, 2026
@mhucka mhucka marked this pull request as ready for review January 6, 2026 21:26
@mhucka mhucka requested a review from sergeisakov January 6, 2026 21:26
Copy link
Collaborator

@sergeisakov sergeisakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. However, there are some old CPUs that support avx2 and don't support bmi2. Perhaps we can discontinue support for these CPUs given their limited usage.

mhucka added 3 commits January 9, 2026 04:58
It's unclear that adding test logic to detect bmi2 would be the right
thing to do in this particular Makefile. For the time being, I'm
reverting this back to how it was before this PR.
Per discussion in review, it is possible for some older processors to
support AVX2 but not BMI2, so those should be tested separately.

Also, this slightly reorganizes the flags code to put things in
alphabetical order, and also adds the flags to the `print-vars` target.
This adds a check for whether the host supports BMI2, and only adds the
flag if so.
@github-actions github-actions bot added size: S 10< lines changed <50 and removed Size: XS <10 lines changed labels Jan 13, 2026
The bazel build files need to be reworked. Let's make this PR be about
the cmake & make files.
@mhucka
Copy link
Collaborator Author

mhucka commented Jan 13, 2026

LGTM. However, there are some old CPUs that support avx2 and don't support bmi2. Perhaps we can discontinue support for these CPUs given their limited usage.

Thank you again for that tip @sergeisakov. I reworked the makefile & cmake changes so that now it tests if BMI2 is actually supported before it adds the flag. Could you take another look at the new version of this PR when you get a chance?

@mhucka mhucka requested a review from sergeisakov January 13, 2026 05:26
Copy link
Collaborator

@sergeisakov sergeisakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

add_compile_options($<$<NOT:$<CONFIG:Debug>>:/O2>)
else()
include(CheckCCompilerFlag)
check_c_compiler_flag("-mbmi2" COMPILER_HAS_BMI2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That just checks if the compiler accepts this flag. BMI2 might still not be supported by CPU. I think this is okay for now. Perhaps we should have two AVX2 versions (with BMI2 and without) and detect one at runtime as we now detect SSE/AVX2/AVX512. We can add the second version later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That just checks if the compiler accepts this flag. BMI2 might still not be supported by CPU. I think this is okay for now. Perhaps we should have two AVX2 versions (with BMI2 and without) and detect one at runtime as we now detect SSE/AVX2/AVX512. We can add the second version later.

Yeah, I confess I used the compiler flags because I couldn't find a cmake construct to do it properly. Of course you're right, it's incorrect and lazy.

So I searched for a better way. I found some alternatives, but they are more involved and IMHO better left to a separate PR. The simplest solution for this PR seems to be to use grep as is done in tests/Makefile. The latest push to this PR has the change to the CMakeLists.txt to do it that way instead of checking compiler flags.

@sergeisakov sorry to ask you to take yet another look …

As Sergei commented in the PR review, using the cmake test for compiler
options is not the right way to test for BMI2 support in the CPU. Change
it to use the same approach used in the Makefile in the tests/ directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S 10< lines changed <50

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants